/**
 * @Authorv Johann Prieur
 */
package mygui;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;

import kernel.Simulator;
import kernel.DataObstacle;
import objects.Obstacle;

import utils.*;

public class ObstacleProperties extends Properties {

    private Simulator _kernel;
    private Obstacle _obstacle;
    private DataObstacle _data;
    private GeneralPanel _general;

    public ObstacleProperties(Obstacle obstacle)
    {
	_kernel = Simulator.getSimulator();
	_obstacle = obstacle;
	_data = (DataObstacle)_obstacle.getProperties();
	_general = new GeneralPanel(_kernel, _data, _obstacle);

	setTitle("Obstacle properties");

	add("North", new ObjectIdCard(Type.obstacle,
				      _data.getName(),
				      "Obstacle"));
	
	add("Center", _general);
	pack();
	WindowUtilities.centerFrame(this);
	setVisible(true);
    }
    
    public void apply()
    {
	_general.apply();
	_obstacle.setProperties(_data);
    }

    private class GeneralPanel extends JPanel 
    {
	private Simulator _kernel;
	private Obstacle _obstacle;
	private DataObstacle _data;

        private JTextField _name;
	private String _name_tip = "Name of the obstacle";

	/**
	 * Physical resistance of the obstacle
	 */
	private JSpinner _braking;
	private String _braking_tip = "Physical resistance of the obstacle";
	/**
	 * Wave resistance of the obstacle
	 */
	private JSpinner _wavebraking;
	private String _wavebraking_tip = "Wave resistance of the obstacle";

	private GeneralPanel(Simulator kernel,
			     DataObstacle data, 
			     Obstacle obstacle)
	{
	    _kernel = kernel;
	    _obstacle = obstacle;
	    _data = data;
	    
	    setLayout(new GridLayout(3,2,4,4));
	    setBorder(new EmptyBorder(4,4,4,4));

	    _name = new JTextField(_data.getName());

	    _braking = new JSpinner(new SpinnerNumberModel(_data.getAlpha(), // initial
							   DataObstacle.ALPHA_MIN, // min
							   DataObstacle.ALPHA_MAX, // max
							   0.01)); // increment
	    

	    _wavebraking = new JSpinner(new SpinnerNumberModel(_data.getGamma(), // initial
								DataObstacle.GAMMA_MIN, // min
								DataObstacle.GAMMA_MAX, // max
								0.01)); // increment
	    add(new JLabel("Name"));
	    add(_name);
	    _braking.setToolTipText(_name_tip);

	    add(new JLabel("Physical braking"));
	    add(_braking);
	    _braking.setToolTipText(_braking_tip);

	    add(new JLabel("Wave braking"));
	    add(_wavebraking);		
	    _wavebraking.setToolTipText(_wavebraking_tip);
								
	}

	public void apply()
	{
	    _data.setName(_name.getText());
	    _data.setAlpha(((Double)_braking.getValue()).doubleValue());
	    _data.setGamma(((Double)_wavebraking.getValue()).doubleValue());
	}
    }
    
}